﻿
$(document).ready( function() {
	
	count = document.getElementById("count").value;
	allow = 5

	
	// Show menu when #myDiv is clicked
	$("#icons DIV").contextMenu({
		menu: 'iconMenu'
	},
		function(action, el, pos) {
		
		 /* alert(
			'Action: ' + action + '\n\n' +
			'Element ID: ' + $(el).attr('id') + '\n\n' + 
			'X: ' + pos.x + '  Y: ' + pos.y + ' (relative to element)\n\n' + 
			'X: ' + pos.docX + '  Y: ' + pos.docY+ ' (relative to document)'
			); */
			
			
			  var full = $(el).attr('id')
			  var a = full.lastIndexOf('(')
			  var b = full.lastIndexOf(')')
			   
			  var a = a + 1
			  var id = full.substring(a,b)
			
			  if(action == "delete") { deleteIcon(id); }
			  if(action == "open")   { openWindow(id); }
			  if(action == "asign") { openWindow(2); }
			
			
	});
	
	// Show menu when a list item is clicked
	$("#myList UL LI").contextMenu({
		menu: 'iconMenu'
	}, function(action, el, pos) {
		alert(
			'Action: ' + action + '\n\n' +
			'Element text: ' + $(el).text() + '\n\n' + 
			'X: ' + pos.x + '  Y: ' + pos.y + ' (relative to element)\n\n' + 
			'X: ' + pos.docX + '  Y: ' + pos.docY+ ' (relative to document)'
			);
	});
	
    // Show menu when a list item is clicked
	$("#background").contextMenu({
		menu: 'defaultMenu'
	}, function(action, el, pos) {
		 
		 if(action == "wall")  { openWindow(1); }
		 if(action == "user") { createUser(); }
		 if(action == "login")  { loginUser();  }
		 if(action == "refresh") {  document.location.reload(); }
		 if(action == "reloads") { document.cookie = "desktop=; expires=";  document.location.reload(); }
		 if(action == "file")    { openWindow(0) }
		 if(action == "icon")    { 
		 
		  
 		     
		   allow = allow - 1;
		    
		     if(allow == 0) { refresh = confirm("Nepieciešami Pārlādēt Statusu"); 
		       if(refresh) { 
		         //document.cookie = "desktop=; expires="; 
		         document.location.reload();}}
		       
		     else {  
		      
		       var title  = Prompt.show('Nosaukums:')
		 
		    if(title) {
		      
		        insertTo = count - allow; 
		          toper = pos.y - (insertTo * 100);      
		           
		     document.getElementById('dragable('+insertTo+')').style.display = 'block'
		     document.getElementById('dragable('+insertTo+')').style.top = toper + 'px'
		     document.getElementById('dragable('+insertTo+')').style.left = pos.x + 'px'
		     document.getElementById('dragable('+insertTo+')').style.paddingTop = insertTo + '00px'
 		     
 		     document.getElementById('task('+insertTo+')').innerHTML = title;
 		     document.getElementById('iconTitle('+insertTo+')').innerHTML = title;
 		     document.getElementById('windowName('+insertTo+')').innerHTML = title;
 		      		     
 		     document.getElementById('iconImage('+insertTo+')').src = 'images/icons/blanks.png';
 		     document.getElementById('frame('+insertTo+')').src = 'filesystem/blank.html'
 		       
 		     document.getElementById('icon('+insertTo+')').style.display = 'block'  
 		      		       
 		     document.getElementById('systray').src = "icon.aspx?action=add&name=" + title + "&order=" + insertTo;
 		     document.getElementById('systray').src = ""
 		     }
 		      
		
		 }
		   
		}


	});


});
